{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Final Project (Capstone Project)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Financial Analyst Challenge: Analyze and advice on a Client´s portfolio"
   ]
  },
  {
   "attachments": {
    "image.png": {
     "image/png": "[encoded data, 631812 characters]"
    }
   },
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![image.png](attachment:image.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Welcome to the Final Project!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This challenge requires you to __apply__ and __combine__ many concepts and methods that you have learned in this course."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This scenario could be a real-world case in the life of a Financial Analyst / Advisor. It requires a combination of <br>\n",
    "- solid __coding skills__, <br><br>\n",
    "- some __background knowledge in Finance/Investment__ (all you need to know is teached in the course)\n",
    "and, even more important <br><br>\n",
    "   \n",
    "- the ability to __interpret__ and __understand__ the __underlying data and the client´s requests__ and the ability to __incorporate the Client´s inputs__ into your analysis <br> <br>\n",
    "__Combining Expertise / hard Skills with case-specific Judgement__ -> requires some practise but also some talent. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Case"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It´s your first day as Financial Analyst in an Asset Management Firm that manages the Portfolios of High Net Worth Individuals (HNWI). <br><br>\n",
    "Your Boss recently solicited a new Client, Mary Johnson, 65 years old. Marie recently retired after 30 Years as an independent physician. Her husband died soon and bequeathed a considerable Investment Portfolio to her. <br><br>\n",
    "From 1997 until today she has been solely invested into __5 Health Care Stocks__, that are today the __most valuable__ Health Care Stocks in the US Market in terms of __Market Capitalization__. She strongly believes that it´s best to only invest into sectors where she has specific industry expertise (Health Care). She only trusts the largest and best-known companies in the market. Her Strategy has been pretty simple: Buying an __equal number of shares__ of each Stock and __reinvesting all dividends__ (assume no costs and taxes). <br> <br> \n",
    "Now that she has retired with no active work income (she needs to live from her Investment Portfolio), Mary decided to make her portfolio more suitable to the new situation. In a very first step, she agreed with your boss to sell parts of her five-Stocks Portfolio and invest into lower risk Government Bonds and high-quality Corporate Bonds.   <br> <br>\n",
    "Now, as a next second step, it´s your task to guide her into a __more diversified Stock Portfolio that better reflects Modern Portfolio Theory__.   "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "---------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Now it´s your Turn!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 1: Identify the five stocks and get the respective Stock Tickers__! (use the __listings_clean.csv__ file)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 2: Create an appropriate Index from 1997 (hint: start with 1996-12-31) until the end of 2018 that best reflects her strategy and create a normalized Price Chart (with Base Value 100 on 1996-12-31)!__ Hint: Use Yahoo Finance!<br>\n",
    "(At a first glance: Do you think her Portfolio performed well?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mary did not closely monitor her Portfolio in the past. She is a very self-confident, if not overconfident person. Therefore, you suspect that she overestimates her portfolio´s past performance."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 3: Verify the following statement that she made during the first meeting with your boss: \"For investments periods of 6 years or longer, I never lost money with my portfolio!\"__ Is she right? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once you have analyzed and educated Mary on the past Performance of her Portfolio, your task is to compare her Portfolio with similar designed Portfolios/Indexes of other Sectors (five largest companies). Your colleagues already prepared appropriate indexes for other sectors (sector_indexes.csv). "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 4: Analyze and compare the performance of Mary´s Large-Cap Health Care Portfolio with other sectors for the most recent 4-years period from 2015 until 2018. Provide Mary with an interactive normalized Price Chart where she can play around__! <br>\n",
    "Hint: Import and use the csv-file \"__sector_indexes.csv__\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 5: Compare annualized risk and return based on daily (simple) returns and create an appropriate plot! Calculate the Sharpe Ratio and compare!__  Hint: Assume a risk-free Return of 1.3% p.a. <br>\n",
    "(In terms of Sharpe Ratio, how did Mary´s Portfolio perform compared to the other Indexes?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The results of Step 5 support Mary´s belief that the Health Care Sector is the one and only Sector to be invested in: \"Even if the Consumer Services- Sector showed a better Sharpe Ratio and a higher Return than Health Care, the Risk is simply too high!\" And there is no benefit to add other Sectors with lower Sharpe Ratio than Health Care - this will not improve the Sharpe Ratio of my portfolio! Therefore, I will stick to my current Stock Portfolio!\". Your argument __\"Past Performance is no Guarantee / no Indicator of future Results\"__ did not impress her much."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 6:  Show Mary that she could have improved the Sharpe Ratio of her Portfolio by adding other Sectors, without increasing the Total Risk of her Portfolio! Educate her on the Portfolio Diversification Effect!__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Simulate 50,000 random Portfolios (use np.random.seed(111)!) and show the best Portfolio as well as a visual summary of your analysis. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "----------------------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Congrats! With your analysis, Mary agrees to diversify her Stock Portfolio by adding other Sectors! However, she still does not agree with \"Past Performance is no Guarantee / no Indicator of future Results\" and \"The most valuable Stocks might also be the most overvalued Stocks\". Given that Mary is not an easy person and given her considerable Wealth compared to her Lifestyle, you agree with your Boss that you (and Mary´s Portfolio) can live with the results of this first advisory round. Together with Mary, you make the following plan: __Adding those sectors__ to her Stock Portfolio that\n",
    "- Showed a __positive Alpha__ over the most recent 4-Year Period\n",
    "- Have a __Beta-Factor below 1__ (non-cyclical / defensive) as Mary expects/fears some Market Turmoil in the near Future. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "__Step 7: Identify Sectors with positive Alpha and a Beta-Factor < 1. Which Sectors will be added to Mary´s Portfolio?__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Use the S&P 500 Total Return Index as Market Portfolio (SP500_TR.csv)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# STOP HERE, IF YOU WANT TO DO THE CHALLENGE ON YOUR OWN!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Some Guidance and Hints"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 1: Identify the five stocks and get the respective Stock Tickers!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Identify the __five largest stocks__ in the __Sector \"Health Care\"__ in terms of __Market Capitalization__! <br>\n",
    "__Save__ the respective __Stock Tickers/Symbols__ in a list!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the cell!\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import yfinance as yf\n",
    "import cufflinks as cf\n",
    "\n",
    "plt.style.use(\"seaborn\")\n",
    "pd.options.display.float_format = '{:.3f}'.format"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the cell!\n",
    "listings = pd.read_csv(\"listings_clean.csv\", index_col = \"Symbol\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# run the cell!\n",
    "listings.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# run the cell!\n",
    "listings.tail()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Now it´s your Turn!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Your Coding Cells"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 2: Create an appropriate Index from 1997 (start with 1996-12-31) until the end of 2018 that best reflects her strategy and create a normalized Price Chart (with Base Value 100 on 1996-12-31)!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(At a first glance: Do you think Mary´s Portfolio performed well?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Download and import historical prices from Yahoo Finance. Create a Price-weighted Total Return Index! (You need Close Prices and Adjusted Close Prices!) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 3: Verify the following statement that she made during the first meeting with your boss: \"For investments periods of 6 years or longer, I never lost money with my portfolio!\" Is she right? "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Create a Return Triangle with annual log Returns."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 4: Analyze and compare the performance of her Large-Cap Health Care Portfolio with other sectors for the most recent 4-years period from 2015 until 2018. Provide Mary with an interactive normalized Price Chart where she can play around!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Import and use the csv-file \"sector_indexes.csv\". Use an interactive Plotly Chart!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 5: Compare annualized risk and return based on daily (simple) returns and create an appropriate plot! Calculate the Sharpe Ratio and compare! "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "(In terms of Sharpe Ratio, how did Mary´s Portfolio perform compared to the other Indexes?)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Assume a risk-free Return of 1.3% p.a."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#use the following user-defined function:\n",
    "\n",
    "def ann_risk_return(returns_df):\n",
    "    summary = returns_df.agg([\"mean\", \"std\"]).T\n",
    "    summary.columns = [\"Return\", \"Risk\"]\n",
    "    summary.Return = summary.Return*252\n",
    "    summary.Risk = summary.Risk * np.sqrt(252)\n",
    "    return summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#use the following Code to create the plot (adjust the variable names if you used different ones!)\n",
    "\n",
    "summary.plot(kind = \"scatter\", x = \"Risk\", y = \"Return\", figsize = (13,9), s = 50, fontsize = 15)\n",
    "for i in summary.index:\n",
    "    plt.annotate(i, xy=(summary.loc[i, \"Risk\"]+0.002, summary.loc[i, \"Return\"]+0.002), size = 15)\n",
    "plt.xlabel(\"ann. Risk(std)\", fontsize = 15)\n",
    "plt.ylabel(\"ann. Return\", fontsize = 15)\n",
    "plt.title(\"Risk/Return\", fontsize = 20)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "###  Step 6:  Show Mary that she could have improved the Sharpe Ratio of her Portfolio by adding other Sectors, without increasing the Total Risk of her Portfolio! Educate her on the Portfolio Diversification Effect!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Simulate 50,000 random Portfolios (use np.random.seed(111)!) and show the best Portfolio as well as a visual summary of your analysis. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#use the following Code to create the plot (adjust the variable names if you used different ones!)\n",
    "\n",
    "plt.figure(figsize = (15, 9))\n",
    "plt.scatter(port_summary.loc[:, \"Risk\"], port_summary.loc[:, \"Return\"],s= 20, color = \"red\")\n",
    "plt.scatter(summary.loc[:, \"Risk\"], summary.loc[:, \"Return\"], s= 50, color = \"black\", marker = \"D\")\n",
    "plt.xlabel(\"ann. Risk(std)\", fontsize = 15)\n",
    "plt.ylabel(\"ann. Return\", fontsize = 15)\n",
    "plt.title(\"Risk/Return\", fontsize = 20)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "--------------------"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Step 7: Identify Sectors with positive Alpha and a Beta-Factor < 1. Which Sectors will be added to Mary´s Portfolio?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Hint: Use the S&P 500 Total Return Index as Market Portfolio (SP500_TR.csv). Calculate the following Metrics for the Sector Indexes and the Market Portfolio:\n",
    "- Annualized Risk and Return (daily simple Returns)\n",
    "- Sharpe Ratio\n",
    "- Annualized Total Risk in Variance units\n",
    "- Systematic Risk\n",
    "- Unsystematic Risk\n",
    "- Beta\n",
    "- CAPM Return\n",
    "- Alpha"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Well Done! "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
